import { UserInfo } from "@/type/login"
import { isCodeMessage } from "../config"

/**
 * @author: 小佘佘🦀
 * @description 获取验证码
 * @param email 邮箱
 * 
 * */
export const getCode = async (email: string) => {

    const response = await fetch(`/api-backend/api/user/code?email=${email}`, {
        method: "get",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
        },
        mode: "no-cors",
        redirect: "manual" 
    })

    return isCodeMessage(response)
}



/**
 * @author: 小佘佘🦀
 * @description 注册
 * @param user 用户信息
 * */
export const register = async (user: UserInfo) => {

    const params = new URLSearchParams()
    params.append("email", user.email)
    params.append("password", user.password)
    params.append("validity", user.validity)
    params.append("verify_code", user.verify_code)
    params.append("code", user.otp)

    const response = await fetch(`/api-backend/api/user/add`, {
        method: "post",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
        },
        mode: "no-cors",
        body: params.toString()
    })

    if (response.ok) {
        const data = await response.json()
        return data
    }

}